package com.yykj.template.server.controller;

import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import com.yykj.common.util.LocalDateTimeFormatterUtil;
import com.yykj.common.vo.ResultVO;
import com.yykj.prescription.client.dto.PrescriptionDetailDTO;
import com.yykj.prescription.client.dto.PrescriptionMemberDTO;
import com.yykj.prescription.client.dto.PrescriptionSettingDTO;
import com.yykj.prescription.client.dto.PrescriptionTipsDTO;
import com.yykj.prescription.client.enums.PrescriptionType;
import com.yykj.prescription.client.feign.PrescriptionFeignClient;
import com.yykj.template.server.dto.RoleInfoDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ftl")
public class FtlController {
    @GetMapping("/prescription/template/{prescriptionId}")
    public String template(@PathVariable String prescriptionId,ModelMap map) throws IOException {
        ResultVO<PrescriptionDetailDTO> resultVO = prescriptionFeignClient.prescriptionInfo(prescriptionId);
        PrescriptionDetailDTO detail = resultVO.getData();
        if(resultVO.getCode() != 0){
            throw new NormalException(ResultEnums.PARAM_ERROR,resultVO.getMsg());
        }

        Map<Integer,String> roleMap = new HashMap<>();
        Map<Integer,String> picMap = new HashMap<>();
        //审核角色,1:药师，2审方，3调配，4核对，5发药
        List<RoleInfoDTO> roleInfoDTOList = new ArrayList<>();
        List<PrescriptionMemberDTO> memberList = detail.getMemberList();
        for(PrescriptionMemberDTO memberDTO:memberList){
            picMap.put(memberDTO.getRole(),memberDTO.getSign());
        }

        roleMap.put(0,"医生");
        roleMap.put(1,"药师");
        roleMap.put(2,"审方");
        roleMap.put(3,"调配");
        roleMap.put(4,"核对");
        roleMap.put(5,"发药");

        for (int i = 0; i <=5;i++){
            RoleInfoDTO dto = new RoleInfoDTO();
            dto.setRoleId(i);
            dto.setRoleName(roleMap.get(i));
            dto.setUrl(picMap.get(i));
            roleInfoDTOList.add(dto);
        }

        ResultVO<List<PrescriptionTipsDTO>> tipsResultVO = prescriptionFeignClient.listHospitalPrescriptionTips(detail.getHospitalId());
        List<PrescriptionTipsDTO> tipList = null;
        if(tipsResultVO.getCode() == 0){
            tipList = tipsResultVO.getData();
            for (PrescriptionTipsDTO dto :tipList){
                dto.setTips(formatFtlString(dto.getTips(),43));
            }
        }
        map.addAttribute("tips",tipList);

        ResultVO<PrescriptionSettingDTO> settingDTOResultVO = prescriptionFeignClient.viewPrescriptionSetting(detail.getHospitalId());
        PrescriptionSettingDTO settingDTO = null;
        if(settingDTOResultVO.getCode() == 0){
            settingDTO = settingDTOResultVO.getData();
        }
        map.addAttribute("prescriptionSetting",settingDTO);

        String appeal = formatFtlString(detail.getAppeal(),36);
        String conclusion = formatFtlString(detail.getConclusion(),36);
        String prescriptionTime = detail.getAddTime().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
//        String appeal = detail.getAppeal();
//        String conclusion = detail.getConclusion();
        map.addAttribute("detail",detail);
        map.addAttribute("appeal",appeal);
        map.addAttribute("conclusion",conclusion);
        map.addAttribute("roleInfoDTOList",roleInfoDTOList);
        map.addAttribute("prescriptionTime",prescriptionTime);
        if(PrescriptionType.china.equals(detail.getType())){
            map.addAttribute("chinaUseInfo",detail.getChinaUseInfo());
            map.addAttribute("chinaDrugList",detail.getChinaDrugList());
            return "china";
        }else{
            map.addAttribute("westDrugList",detail.getWestDrugList());
            return "west";
        }
    }

    @Resource
    private PrescriptionFeignClient prescriptionFeignClient;

    private static String formatFtlString(String src,int cnt){
        if(StringUtils.isEmpty(src)){
            return "";
        }
        if(src.length()<=cnt){
            return src;
        }else{
            StringBuilder sb = new StringBuilder();
            sb.append(src.substring(0,cnt));
            sb.append("<br/>");
            sb.append(formatFtlString(src.substring(cnt),cnt));
            return sb.toString();
        }
    }

}
